package com.tec.modules.order.service.impl;

import com.tec.modules.order.domain.Stock;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.tec.modules.order.repository.StockRepository;
import com.tec.modules.order.service.StockService;
import com.tec.modules.order.service.dto.StockDto;
import com.tec.modules.order.service.dto.StockQueryCriteria;
import com.tec.modules.order.service.mapper.StockMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author tec
* @date 2020-05-27
*/
@Service
//@CacheConfig(cacheNames = "stock")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StockServiceImpl implements StockService {

    private final StockRepository stockRepository;

    private final StockMapper stockMapper;

    public StockServiceImpl(StockRepository stockRepository, StockMapper stockMapper) {
        this.stockRepository = stockRepository;
        this.stockMapper = stockMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(StockQueryCriteria criteria, Pageable pageable){
        Page<Stock> page = stockRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(stockMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<StockDto> queryAll(StockQueryCriteria criteria){
        return stockMapper.toDto(stockRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public StockDto findById(Long id) {
        Stock stock = stockRepository.findById(id).orElseGet(Stock::new);
        ValidationUtil.isNull(stock.getId(),"Stock","id",id);
        return stockMapper.toDto(stock);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public StockDto create(Stock resources) {
        return stockMapper.toDto(stockRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(Stock resources) {
        Stock stock = stockRepository.findById(resources.getId()).orElseGet(Stock::new);
        ValidationUtil.isNull( stock.getId(),"Stock","id",resources.getId());
        stock.copy(resources);
        stockRepository.save(stock);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            stockRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<StockDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StockDto stock : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("型号", stock.getModel());
            map.put("颜色", stock.getColor());
            map.put("尺码", stock.getSize());
            map.put("男女款", stock.getStyle());
            map.put("库存件数", stock.getTotalQuantity());
            map.put("售出数量", stock.getSoldQuantity());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
